package com.example.apkupdatedemo.download;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import com.example.apkupdatedemo.UpdateDialogActivity;
import com.example.apkupdatedemo.json.Android_ireader;
import com.example.apkupdatedemo.json.Root;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class GetsoftInfo extends AsyncTask<String, Integer, String> {
	private int localVersionCode;
	private String localVersionName;
	private Context mContext;
	
	public GetsoftInfo(Context context,int versionCode,String versionName){
		this.localVersionCode = versionCode;
		this.localVersionName = versionName;
		this.mContext = context;
	}
	
	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		super.onPreExecute();
		
	}

	@Override
	protected String doInBackground(String... arg0) {
		// TODO Auto-generated method stub
		//publishProgress(values)
		return getJsonFromClient(arg0[0]);
	}

	@Override
	protected void onPostExecute(String result) {
		// TODO Auto-generated method stub
		
		handleJson(result);
		super.onPostExecute(result);
	}
	
	private void handleJson(String json){
		Gson mGson = new Gson();
		java.lang.reflect.Type type = new TypeToken<Root>() {}.getType();
		Root root = mGson.fromJson(json, type); // 
		
		Android_ireader apk = root.getAndroid_ireader();
		
		int newVersionCode = apk.getVersion_code();
		
		
		int compareVersionResult = newVersionCode - this.localVersionCode;
		
		if (compareVersionResult > 0) {
			//可以更新
			Intent dialogIntent = new Intent(mContext,UpdateDialogActivity.class);
			dialogIntent.putExtra("versionname",apk.getApp_version());
			dialogIntent.putExtra("content", apk.getFeatures());
			dialogIntent.putExtra("apkurl", apk.getUpdate_url());
			mContext.startActivity(dialogIntent);
		}else{
			Toast.makeText(mContext, "已是最新版本", 0).show();
		}
		
		/*Log.d("zsr", "name: "+apk.getName());
		Log.d("zsr", "versioncode: "+apk.getVersion_code());
		Log.d("zsr", "getFeatures: "+apk.getFeatures());
		Log.d("zsr", "update_url: "+apk.getUpdate_url());*/
	}
	
	

	// 获取json数据
	private String getJsonFromClient(String url) {
		StringBuffer sb = null;
		try {
			URL httpUrl = new URL(url);
			HttpURLConnection mHttpURLConnection = (HttpURLConnection) httpUrl
					.openConnection();
			mHttpURLConnection.setRequestMethod("GET");
			mHttpURLConnection.setReadTimeout(5000);

			BufferedReader br = new BufferedReader(new InputStreamReader(
					mHttpURLConnection.getInputStream(), "utf-8"));

			sb = new StringBuffer();
			String string = null;
			while ((string = br.readLine()) != null) {
				sb.append(string);
			}
			br.close();

		} catch (MalformedURLException e) {
			e.printStackTrace();
			Log.d("zsr","onCancellede: "+e.toString());
		} catch (IOException e) {
			e.printStackTrace();
			Log.d("zsr","onCancellede2: "+e.toString());
		}

		return sb.toString();
	}
	@Override
	protected void onCancelled() {
		// TODO Auto-generated method stub
		super.onCancelled();
		Log.d("zsr","onCancelled");
	}

}
